package cn.edu.neusoft.wuguangsheng.market.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.neusoft.wuguangsheng.market.R;
import cn.edu.neusoft.wuguangsheng.market.bean.RegisterBean;
import cn.edu.neusoft.wuguangsheng.market.listener.TListener;
import cn.edu.neusoft.wuguangsheng.market.model.MySetModel;

public class MySetActivity extends BaseActivity {
    ImageView setRet;
    EditText shname,phone,address;
    TextView setpassword;
    Button myset;
    String phone1,address1,uname1,password1;
    @Override
    void initView() {
        setLayout_file(R.layout.activity_my_set);
        setRet=(ImageView)findViewById(R.id.my_ret);
        shname=(EditText)findViewById(R.id.my_et_uname);
        setpassword=(TextView)findViewById(R.id.setpassword);
        phone=(EditText)findViewById(R.id.my_et_phone);
        address=(EditText)findViewById(R.id.my_et_address);
        myset=(Button)findViewById(R.id.my_btn_set);

        phone1=getIntent().getStringExtra("phone");
        address1=getIntent().getStringExtra("address");
        uname1=getIntent().getStringExtra("shname");

        address.setText(address1);
        phone.setText(phone1);
        shname.setText(uname1);
    }

    @Override
    void initEvent() {
     setRet.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             finish();
         }
     });
        myset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String uid=getid();
                uname1=shname.getText().toString();
                address1=address.getText().toString();
                phone1=phone.getText().toString();



                if(shname.equals("")){
                    Toast.makeText(MySetActivity.this,"收货人不能为空",Toast.LENGTH_SHORT).show();
                }

                else if(address.equals("")){
                    Toast.makeText(MySetActivity.this,"地址不能为空",Toast.LENGTH_SHORT).show();
                }
                else if(!isMobileNO(phone1)){
                    Toast.makeText(MySetActivity.this,"检查电话格式是否正确",Toast.LENGTH_SHORT).show();
                }

                else{
                    MySetModel mySetModel=new MySetModel();
                    mySetModel.setMyInfo(uid,uname1,phone1,address1,tListener);
                }
            }
        });


        setpassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MySetActivity.this,SetPasswordActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    void initDate() {

    }

    TListener<RegisterBean> tListener=new TListener<RegisterBean>() {
        @Override
        public void onResponse(RegisterBean registerBean) {
String success=registerBean.getSuccess();
            if(success.equals("1")){
                Toast.makeText(MySetActivity.this,"修改信息成功",Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(MySetActivity.this,"修改信息失败",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
          Toast.makeText(MySetActivity.this,msg,Toast.LENGTH_SHORT).show();
        }
    };


    public static boolean isMobileNO(String mobiles) {
        //"[1]"代表第1位为数字1，"[358]"代表第二位可以为3、5、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。
        String telRegex = "[1][34578]\\d{9}" ;
        if (TextUtils.isEmpty(mobiles)) return false ;
        else return mobiles.matches( telRegex ) ;
    }
}
